﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【Token刷新记录】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// AutoToken刷新记录
    /// </summary>
    public abstract class AutoRefreshTokenMapping : BaseMapping<RefreshToken, RefreshTokenModel, RefreshTokenFilterModel>
    {
        public AutoRefreshTokenMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override RefreshTokenModel Entity2Model(RefreshToken entity)
        {
            RefreshTokenModel model = new RefreshTokenModel();

            model.RefreshTokenID = entity.RefreshTokenID;
            model.LoginCode = entity.LoginCode;
            model.LoginType = entity.LoginType;
            model.LoginTypeID = entity.LoginTypeID;
            model.UserID = entity.UserID;
            model.TokenValue = entity.TokenValue;
            model.IssuedOn = entity.IssuedOn;
            model.ExpiredOn = entity.ExpiredOn;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override RefreshToken Model2Entity(RefreshTokenModel model)
        {
            RefreshToken entity = new RefreshToken();

            entity.RefreshTokenID = model.RefreshTokenID;
            entity.LoginCode = model.LoginCode;
            entity.LoginType = model.LoginType;
            entity.LoginTypeID = model.LoginTypeID;
            entity.UserID = model.UserID;
            entity.TokenValue = model.TokenValue;
            entity.IssuedOn = model.IssuedOn;
            entity.ExpiredOn = model.ExpiredOn;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<RefreshToken> FilterModel2Where(RefreshTokenFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._RefreshTokenID.HasValue)
            {
                where = where.And(x => x.RefreshTokenID == filterModel._RefreshTokenID.Value);
            }
            if (filterModel._RefreshTokenIDs.IsNotEmpty() && filterModel._RefreshTokenIDs.Count > 0)
            {
                where = where.And(x => filterModel._RefreshTokenIDs.Contains(x.RefreshTokenID));
            }
            if (filterModel._NotRefreshTokenIDs.IsNotEmpty() && filterModel._NotRefreshTokenIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRefreshTokenIDs.Contains(x.RefreshTokenID));
            }

            //用户登录编码
            if (!string.IsNullOrEmpty(filterModel._LoginCode))
            {
                where = where.And(x => x.LoginCode.Contains(filterModel._LoginCode));
            }
            if (!string.IsNullOrEmpty(filterModel._LoginCodeEqual))
            {
                where = where.And(x => x.LoginCode == filterModel._LoginCodeEqual);
            }

            //用户登录类型
            if (filterModel._LoginType.HasValue)
            {
                where = where.And(x => x.LoginType == filterModel._LoginType.Value);
            }
            if (filterModel._LoginTypes.IsNotEmpty() && filterModel._LoginTypes.Count > 0)
            {
                where = where.And(x => filterModel._LoginTypes.Contains(x.LoginType));
            }
            if (filterModel._NotLoginTypes.IsNotEmpty() && filterModel._NotLoginTypes.Count > 0)
            {
                where = where.And(x => !filterModel._NotLoginTypes.Contains(x.LoginType));
            }

            //记录时间
            if (filterModel._IssuedOn.HasValue)
            {
                where = where.And(x => x.IssuedOn == filterModel._IssuedOn.Value);
            }
            if (filterModel._IssuedOn_Begin.HasValue)
            {
                where = where.And(x => x.IssuedOn >= filterModel._IssuedOn_Begin.Value);
            }
            if (filterModel._IssuedOn_End.HasValue)
            {
                where = where.And(x => x.IssuedOn < filterModel._IssuedOn_End.Value.AddDays(1));
            }

            //过期时间
            if (filterModel._ExpiredOn.HasValue)
            {
                where = where.And(x => x.ExpiredOn == filterModel._ExpiredOn.Value);
            }
            if (filterModel._ExpiredOn_Begin.HasValue)
            {
                where = where.And(x => x.ExpiredOn >= filterModel._ExpiredOn_Begin.Value);
            }
            if (filterModel._ExpiredOn_End.HasValue)
            {
                where = where.And(x => x.ExpiredOn < filterModel._ExpiredOn_End.Value.AddDays(1));
            }

            return where;
        }
        #endregion
    }
}

